package com.boilerCloud.controller.maintenance;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.boilerCloud.dataSource.LogAnnotation;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.maintenance.MaintenancePlan;
import com.boilerCloud.dto.sys.ReqResult;
import com.boilerCloud.dto.sys.User;
import com.boilerCloud.dto.work.WorkOrder;
import com.boilerCloud.service.log.ILogService;
import com.boilerCloud.service.maintenance.IMaintenanceService;


@RestController
@RequestMapping("/maintenance/")
public class MaintenanceControler {
	private static final Logger log = LoggerFactory.getLogger(MaintenanceControler.class);
	@Resource
	private IMaintenanceService service;

	
	@Resource
	private ILogService logService;

	@RequestMapping(value = "maintenanceList")
	public ModelAndView modelCodeList() {
		return new ModelAndView("/views/after/maintenancePlan.html");
	}
	

	/**
	 * 查询维保计划列表
	 * @param grade
	 * @param yema
	 * @param length
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "qryMaintenancePlanList", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult qryMaintenancePlanList(@ModelAttribute MaintenancePlan model ,
			@RequestParam(required = true) int yema,
			@RequestParam(required = true) int length,HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			               
			HzlqswPage<MaintenancePlan> page = new HzlqswPage<MaintenancePlan>();
			page.setCurPage(yema);
			page.setPageSize(length);
			service.qryList(model,page);
			rst.setReturnObject(page);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("查询维保计划列表失败");
			log.error("查询维保计划列表异常",e);
			return rst;
		}
	}
	
	/**
	 * 修改维保计划信息
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("修改维保计划")
	@RequestMapping(value = "editMaintenancePlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult editMaintenancePlan(
			@ModelAttribute  MaintenancePlan model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.edit(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			logService.addLog(request, "修改维保计划", "修改维保计划" +model.getName(), model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("修改维保计划信息失败");
			log.error("修改维保计划信息异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "getMaintenancePlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult getMaintenancePlan(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			MaintenancePlan model = service.getById(id);
			rst.setResultCode(ReqResult.resultCode_Success);
			rst.setReturnObject(model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("获取维保计划信息失败");
			log.error("获取维保计划信息异常",e);
			return rst;
		}
	}
	
	/**
	 * 新增维保计划
	 * @param device
	 * @param request
	 * @return
	 */
	@LogAnnotation("新增维保计划")
	@RequestMapping(value = "addMaintenancePlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult addMaintenancePlan(
			@ModelAttribute  MaintenancePlan model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.save(model);
			rst.setResultCode(ReqResult.resultCode_Success);
			logService.addLog(request, "新增维保计划", "新增维保计划" +model.getName(), model);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("新增维保计划失败");
			log.error("新增维保计划异常",e);
			return rst;
		}
	}
	
	/**
	 * 删除维保计划
	 * @param businessNo
	 * @param request
	 * @return
	 */
	@LogAnnotation("删除维保计划")
	@RequestMapping(value = "delMaintenancePlan", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult delMaintenancePlan(
			@RequestParam(required = true) int id,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			service.del(id);
			logService.addLog(request, "删除维保计划", "删除维保计划" +id, id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("删除维保计划失败");
			log.error("删除维保计划异常",e);
			return rst;
		}
	}
	
	@RequestMapping(value = "saveWorkOrder", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
	public ReqResult saveWorkOrder(
			@ModelAttribute  WorkOrder model ,
			HttpServletRequest request) {
		ReqResult rst = new ReqResult();
		try {
			User user = (User)request.getSession().getAttribute("usersession");
			model.setCreatorId(user.getId());
			model.setCreatorName(user.getName());
			service.saveWorkOrder(model);
			//logService.addLog(request, "维保派单", "维保派单" +id, id);
			rst.setResultCode(ReqResult.resultCode_Success);
			return rst;


		} catch (Exception e) {
			rst.setResultCode(ReqResult.resultCode_error);
			rst.setReturnObject("维保派单失败");
			log.error("维保派单",e);
			return rst;
		}
	}

	

}
